/* eslint-disable no-control-regex */
export const delimiter = '\x01\x02';

export function warnEventId({ id, taskType, affects }) {
	if (!id || !taskType || !affects) {
		throw new Error(
			`warnEventId field is empty. id: ${id}, taskType: ${taskType}, affects: ${affects}`
		);
	}
	const eventId = affects
		.toString()
		.replace(/\\/g, '\x03')
		.replace(/\$/g, '\x04')
		.replace(/\./g, '\x05');
	return `${taskType}${delimiter}${id}${delimiter}${eventId}`;
}

export function warnId({ id, taskType }) {
	return `${taskType}${delimiter}${id}`;
}

export function warnIdSplit(str) {
	if (typeof str !== 'string') return {};
	const [taskType, id, eventId] = str.split(delimiter);
	return {
		taskType,
		id,
		eventId: (eventId || '')
			.replace(/\x05/g, '.')
			.replace(/\x04/g, '$')
			.replace(/\x03/g, '\\'),
	};
}
